unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, XPMan, ImgList, IniFiles;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    ImageList1: TImageList;
    StatusBar1: TStatusBar;
    SaveDialog1: TSaveDialog;
    procedure Button4Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure lvRepaint;
  end;

var
  Form1: TForm1;

implementation

uses Unit2, Unit3, Unit4, Unit5, Unit6;

{$R *.dfm}

procedure TForm1.lvRepaint;
var
  F:TSearchRec;
  Attr:Integer;
begin
  ListView1.Items.Clear;
  Attr:=faReadOnly+faArchive;
  FindFirst(Dir+'CKeys\*.*',Attr,F);
  if F.Name<>'' then
  begin
    ListView1.AddItem(F.Name,nil);
    while FindNext(F)=0 do
      ListView1.AddItem(F.Name,nil);
  end;
  FindClose(F);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.Show;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Form4.ShowModal;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  if (ListView1.ItemIndex=-1) or (MessageBox(0, PChar('Confirm that you want '+
        'delete key "'+ListView1.ItemFocused.Caption+'"'), 'Delete user''s key', +mb_YesNo +mb_ICONWARNING) <> 6) then
  exit;
  DeleteFile(Dir+'CKeys\'+ListView1.ItemFocused.Caption);
  Form1.lvRepaint;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  Close;
end;

procedure TForm1.Button5Click(Sender: TObject);
var
  {a:array [0..255] of char;
  s:AnsiString;
  f1,f2:file;
  Stream:TMemoryStream;  }
  Ini:TIniFile;
  Suid:String;
begin
  if not Form5.CorrectSUID then
  begin
    beep();
    ShowMessage('The SUID doesn''t exists. To export your public key you must first generate it (My Keyset -> Manage SUID).');
    exit;
  end;
  if not FileExists(Dir+'Keyset\public.rsa') then
  begin
    beep();
    ShowMessage('The keyset doesn''t exists. To export your public key you must first generate it (My Keyset -> Gen New Pair).');
    exit;
  end;
  if (ListView1.ItemIndex=-1){ or (not SaveDialog1.Execute)} then
  begin
    beep;
    exit;
  end;

  Ini:=TINIFile.Create(ExtractFilePath(paramstr(0))+'servers.ini');
  Suid:=Ini.ReadString('server0','SUID','');
  Ini.Free;

  Form6.Label4.Caption:=Suid;
  Form6.Label5.Caption:=ListView1.ItemFocused.Caption;
  
  (*
  Stream:=TMemoryStream.Create;
  {
  I have wondered by this new Delphi'10 typecasts...
  }
  FillChar(a,256,0);
  s:=AnsiString(ListView1.ItemFocused.Caption);
  lstrcpyA(@a,PAnsiChar(s));

  AssignFile(f2,Dir\+'Keyset\public.rsa');
  Reset(f2,1);
  Stream.SetSize(FileSize(f2));
  BlockRead(f2,Stream.Memory^,Stream.Size);
  CloseFile(f2);

  AssignFile(f1,SaveDialog1.FileName);
  Rewrite(f1,1);
  BlockWrite(f1,a,256);
  BlockWrite(f1,Stream.Memory^,Stream.Size);
  CloseFile(f1);
  Exit;

  Stream.Free;
  //Get cUID
  //Write to filename 255 of buf
  //Write there public key
  *)
  Form6.ShowModal;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  lvRepaint;
end;

end.

{
(c) Dea7hmetal, The Coder of Doom
}
